package com.coship.vas.dapg.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author 905305
 * 
 */
public class RequestUtil {
	/**
	 * 
	 * @param regexRule
	 * @param value
	 * @return
	 */
	public static boolean regex(String regexRule, String value) {
		Pattern pattern = Pattern.compile(regexRule);
		Matcher matcher = pattern.matcher(value);
		return matcher.find();
	}

	/**
	 * 判断字符串str是否匹配长度
	 */
	public static boolean strLenCompare(String str, int len) {
		if (str == null) {
			return false;
		} else {
			if (str.length() >= len) {
				return false;
			} else {
				return true;
			}
		}
	}

	/**
	 * 日期字符串格式化
	 * 
	 * @param sStr
	 * @return
	 */
	public static Date strToDate(String sStr) {
		if (sStr == null)
			return null;
		SimpleDateFormat formatter;
		if (sStr.length() == 19) {
			formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else if (sStr.length() == 10) {
			formatter = new SimpleDateFormat("yyyy-MM-dd");
		} else if (sStr.length() == 8) {
			formatter = new SimpleDateFormat("yyyyMMdd");
		} else if (sStr.length() == 14) {
			formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		} else {
			formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		}
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(sStr, pos);
	}

	public static boolean dateCompare(String startTime, String endTime) {
		if (startTime == null || endTime == null) {
			return false;
		}
		return true;
	}

	/**
	 * 解析http参数 split = "&amp;"||"&"
	 * 例：http://172.21.11.171:8888/am/media/getSubCatalog
	 * ?version=1&amp;businessCode
	 * =&amp;catalogCode=111&amp;pageRowCount=1&amp;curPage=1
	 */
	public static String ParseHTTP(String requestStr, String paramName,
			String split) {
		if (StringUtils.isBlank(requestStr) || StringUtils.isBlank(paramName)) {
			return "";
		}
		String subStr = "";
		int startIndex = requestStr.indexOf("?");
		if (startIndex == -1) {
			return "";
		}
		subStr = requestStr.substring(startIndex + 1);
		// 以&amp;截取或以&截取
		if (StringUtils.isBlank(split)) {
			split = "&amp;";
		} else if (StringUtils.isBlank(split)) {
			split = "&";
		}
		int splitStrLen = split.length();
		if (subStr.length() > splitStrLen) {
			subStr = subStr.substring(splitStrLen);
		} else {
			return "";
		}
		String paramters[] = subStr.split(split);
		Map<String, String> paramMap = new HashMap<String, String>();
		for (String parm : paramters) {
			String keyValue[] = parm.split("=");
			if (keyValue.length < 2) {
				paramMap.put(keyValue[0], "");
			} else {
				paramMap.put(keyValue[0], keyValue[1]);
			}

		}
		return paramMap.get(paramName);
	}

	public static void main(String[] args) {
		String test1 = "http://172.21.11.171:8888/am/media/getSubCatalog?version=1&amp;businessCode=&amp;catalogCode=111&amp;pageRowCount=1&amp;curPage=1";
		System.out.println(ParseHTTP(test1, "catalogCode", "&amp;"));
		String test2 = "http://172.21.11.171:8888/am/media/getSubCatalog?version=1&businessCode=&catalogCode=111&pageRowCount=1&curPage=1";
		System.out.println(ParseHTTP(test2, "catalogCode", "&"));
	}
}
